package com.joey.cloud.auth.sys.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joey.cloud.auth.form.PwdRegisterForm;
import com.joey.cloud.auth.sys.entity.SysAccount;
import com.joey.cloud.auth.sys.mapper.SysAccountMapper;
import com.joey.cloud.auth.sys.service.ISysAccountService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 账号信息表  服务实现类
 * </p>
 *
 * @author Joey
 * @since 2021-01-06
 */
@Service
public class SysAccountServiceImpl extends ServiceImpl<SysAccountMapper, SysAccount> implements ISysAccountService {
    @Resource
    SysAccountMapper sysAccountMapper;




    @Override
    public SysAccount queryByName(String accountNum) {
        QueryWrapper<SysAccount> qw1 =  new QueryWrapper();
        qw1.eq("accountNum",accountNum.trim());
        SysAccount account = sysAccountMapper.selectOne(qw1);
        return account;
    }

    @Override
    public void saveByRegister(Long psnId, PwdRegisterForm form) {
        SysAccount sa = new SysAccount();
        sa.setId(IdUtil.getSnowflake(1,1).nextId());
        sa.setAccountNum(form.getAccountNum().trim());
        sa.setPassword(SecureUtil.md5(form.getPassword().trim()));
        sa.setPsnId(psnId);
        sa.setCreateTime(LocalDateTime.now());
        sysAccountMapper.insert(sa);

    }
}
